#include <stdio.h>

char userName[20][20] = {0};       // 预约人姓名
char roomName[4][20] = {"大对子", "清一色", "龙七对", "清大对"};
float roomPrice[4] = {15.0, 20.0, 50.0, 30.0};  // 每小时价格
float totalPrice[20] = {0.0};      // 订单总价
int orderStatus[20] = {0};         // 订单状态
int orderRoom[20] = {0};           // 预约的房间编号
int startTime[20] = {0};           // 开始时间
int duration[20] = {0};            // 持续小时数
int orderCount = 0;                // 当前订单数
int roomStatus[4][24] = {0};       // 房间状态
int avCount = 4;                   // 空余房间数

// 初始化系统
void initSystem() {
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 24; j++) {
            roomStatus[i][j] = 0;
        }
    }
    avCount = 4;
}


int isRoomAvailable(int roomNum, int start, int hours) {
    for (int i = start; i < start + hours; i++) {
        if (roomStatus[roomNum][i] == 1) {
            return 0;
        }
    }
    return 1;
}


void updateAvailableRooms() {
    int newAvCount = 0;
    for (int i = 0; i < 4; i++) {
        int isAvailable = 1;
        for (int j = 0; j < 24; j++) {
            if (roomStatus[i][j] == 1) {
                isAvailable = 0;
                break;
            }
        }
        if (isAvailable) newAvCount++;
    }
    avCount = newAvCount;
}

// 1. 预约麻将室
void reserveRoom() {
    if (orderCount >= 20) {
        printf("预约满了，无法继续预约！\n");
        return;
    }
    
    printf("请输入您的姓名: ");
    scanf("%19s", userName[orderCount]);  
    printf("当前可预约房间:\n");
    printf("房间编号 房间名称 价格(元/小时)\n");
    int availableRooms[4];
    int availableCount = 0;
    
    for (int i = 0; i < 4; i++) {
        int isAvailable = 1;
        for (int j = 0; j < 24; j++) {
            if (roomStatus[i][j] == 1) {
                isAvailable = 0;
                break;
            }
        }


        if (isAvailable) {
            availableRooms[availableCount] = i;
            printf("%d %s %.2f\n", i+1, roomName[i], roomPrice[i]);
            availableCount++;
        }
    }
    
    if (availableCount == 0) {
        printf("抱歉，暂时没有空余房间！\n");
        return;
    }
    
    int roomNum;
    printf("请选择要预约的房间编号: ");
    if (scanf("%d", &roomNum) != 1) {
        printf("输入无效！\n");

        while (getchar() != '\n');
        return;

    }
    roomNum--;
    
    int isValidRoom = 0;

    for (int i = 0; i < availableCount; i++) {
        if (availableRooms[i] == roomNum) {

            isValidRoom = 1;
            break;
        }
    }
    
    if (!isValidRoom) {
        printf("无效的房间编号！\n");
        return;
    }
    
    int start, hours;
    printf("请输入预约开始时间(0-23): ");
    if (scanf("%d", &start) != 1) {
        printf("输入无效！\n");
        while (getchar() != '\n');
        return;
    }
    
    printf("请输入预约时长(小时): ");
    if (scanf("%d", &hours) != 1) {
        printf("输入无效！\n");
        while (getchar() != '\n');
        return;
    }
    
    if (start < 0 || start >= 24 || hours < 1 || start + hours > 24) {
        printf("预约时间无效！\n");
        return;
    }
    
    if (!isRoomAvailable(roomNum, start, hours)) {
        printf("该时间段已被预约！\n");
        return;
    }
    
   
    for (int i = start; i < start + hours; i++) {
        roomStatus[roomNum][i] = 1;
    }
    

    orderRoom[orderCount] = roomNum;

    startTime[orderCount] = start;
    duration[orderCount] = hours;
    totalPrice[orderCount] = roomPrice[roomNum] * hours;
    orderStatus[orderCount] = 1; 
    
    updateAvailableRooms();
    
    printf("预约成功！\n");
    printf("订单号: %d\n", orderCount + 1);
    printf("预约人: %s\n", userName[orderCount]);
    printf("房间名称: %s\n", roomName[roomNum]);
    printf("开始时间: %d:00\n", start);
    printf("时长: %d小时\n", hours);
    printf("总价: %.2f元\n", totalPrice[orderCount]);
    
    orderCount++;
}

// 2. 查看预约
void viewReservations() {
    if (orderCount == 0) {
        printf("暂无预约信息！\n");
        return;
    }
    
    printf("订单号 预约人 房间名称 开始时间 时长(小时) 总价(元) 状态\n");
    for (int i = 0; i < orderCount; i++) {
        printf("%d %-8s %-8s %2d:00 %2d %7.2f ",
               i+1,
               userName[i],
               roomName[orderRoom[i]],
               startTime[i],
               duration[i],
               totalPrice[i]);
        
        if (orderStatus[i] == 1) {
            printf("已预约\n");
        } else {
            printf("已取消\n");
        }
    }
}

// 3. 取消预约
void cancelReservation() {
    if (orderCount == 0) {
        printf("暂无预约信息！\n");
        return;
    }
    
    int orderNum;
    printf("请输入要取消的订单号: ");
    if (scanf("%d", &orderNum) != 1) {
        printf("输入无效！\n");
        while (getchar() != '\n');
        return;
    }
    
    if (orderNum < 1 || orderNum > orderCount) {
        printf("无效的订单号！\n");
        return;
    }
    
    orderNum--;
    
    if (orderStatus[orderNum] == 0) {
        printf("该订单已取消，无法再次取消！\n");
        return;
    }
    
    int roomNum = orderRoom[orderNum];
    int start = startTime[orderNum];
    int hours = duration[orderNum];
    
    
    for (int i = start; i < start + hours; i++) {
        roomStatus[roomNum][i] = 0;
    }
    
    orderStatus[orderNum] = 0; 
    updateAvailableRooms();
    
    printf("订单取消成功！\n");
}

// 4. 查看空余麻将室
void viewAvailableRooms() {
    printf("当前空余房间数: %d\n", avCount);
    
    if (avCount == 0) {
        printf("抱歉，暂时没有空余房间！\n");
        return;
    }
    
    printf("可预约房间详情:\n");
    printf("房间编号 房间名称 价格(元/小时)\n");
    
    for (int i = 0; i < 4; i++) {
        int isAvailable = 1;
        for (int j = 0; j < 24; j++) {
            if (roomStatus[i][j] == 1) {
                isAvailable = 0;
                break;
            }
        }
        if (isAvailable) {
            printf("%d %-8s %.2f\n", i+1, roomName[i], roomPrice[i]);
        }
    }
}

// 5. 我要续订
void renewReservation() {
    if (orderCount == 0) {
        printf("暂无预约信息！\n");
        return;
    }
    
    int orderNum;
    printf("请输入要续订的订单号: ");
    if (scanf("%d", &orderNum) != 1) {
        printf("输入无效！\n");
        while (getchar() != '\n');
        return;
    }
    
    if (orderNum < 1 || orderNum > orderCount) {
        printf("无效的订单号！\n");
        return;
    }
    
    orderNum--;
    
    if (orderStatus[orderNum] == 0) {
        printf("该订单已取消，无法续订！\n");
        return;
    }
    
    int roomNum = orderRoom[orderNum];
    int oldStart = startTime[orderNum];
    int oldHours = duration[orderNum];
    int oldEnd = oldStart + oldHours;
    
    printf("当前订单信息:\n");
    printf("房间名称: %s\n", roomName[roomNum]);
    printf("原预约时间: %d:00-%d:00\n", oldStart, oldEnd);
    
    int newHours;
    printf("请输入要续订的时长(小时): ");
    if (scanf("%d", &newHours) != 1) {
        printf("输入无效！\n");
        while (getchar() != '\n');
        return;
    }
    
    if (oldEnd + newHours > 24) {
        printf("续订时间超出当日范围！\n");
        return;
    }
    
    if (!isRoomAvailable(roomNum, oldEnd, newHours)) {
        printf("该续订时间段已被预约！\n");
        return;
    }
    
   
    for (int i = oldEnd; i < oldEnd + newHours; i++) {
        roomStatus[roomNum][i] = 1;
    }
    
    
    duration[orderNum] += newHours;
    totalPrice[orderNum] += roomPrice[roomNum] * newHours;
    
    printf("续订成功！\n");
    printf("新的预约时间: %d:00-%d:00\n", oldStart, oldEnd + newHours);
    printf("新的总价: %.2f元\n", totalPrice[orderNum]);
}

// 定义清除输入缓冲区的函数
void clear() {
    while (getchar() != '\n');
}

int main() {
    initSystem();
    int choice;
    
    while(1) {
        printf("\n欢迎使用【麻吗】麻将室预约系统\n");
        printf("===========================================\n");
        printf("1. 预约麻将室\n");
        printf("2. 查看预约\n");
        printf("3. 取消预约\n");
        printf("4. 查看空余麻将室\n");
        printf("5. 我要续订\n");
        printf("6. 退出系统\n");
        printf("===========================================\n");
        printf("请输入您的选择: ");
        
        if (scanf("%d", &choice) != 1) {
            printf("无效的选择，请重新输入！\n");
            clear();
            continue;
        }
        clear();
        
        switch (choice) {
            case 1:
                reserveRoom();
                break;
            case 2:
                viewReservations();
                break;
            case 3:
                cancelReservation();
                break;
            case 4:
                viewAvailableRooms();
                break;
            case 5:
                renewReservation();
                break;
            case 6:
                printf("感谢使用【麻吗】麻将室预约系统，再见！\n");
                return 0;
            default:
                printf("无效的选择，请重新输入！\n");
        }
        
        printf("\n按回车键返回主菜单...");
        clear();
    }
    
    return 0;
}
